﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Telerik.Windows.Controls;
using BrokerBase.Lib.Tool;
using Broker.Model;
using NHibernate;

namespace Broker
{
    /// <summary>
    /// Interaction logic for MainNewForm.xaml
    /// </summary>
    public partial class MainNewForm
    {
        public MainNewForm()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainNewForm_Loaded);
            Closed += new EventHandler<WindowClosedEventArgs>(MainNewForm_Closed);
        }

        void MainNewForm_Closed(object sender, WindowClosedEventArgs e)
        {
            App.Current.Shutdown();
        }

        void MainNewForm_Loaded(object sender, RoutedEventArgs e)
        {

            try
            {
                AppGlobal.DataWCF.ClientRegInfoChanged += new Update.ClientRegInfoChangedHandler(DataWCF_ClientRegInfoChanged);

                bool isSuccess = Login();

                ISession sess = AppGlobal.HbmSessionFactory.GetCurrentSession();

                if (isSuccess)
                {
                    Broker.View.LibGlobal.MainWin = this;
                    LoadInfo();
                }
                else
                {
                    Close();
                }

                pgPlatform.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        private void LoadInfo()
        {
            try
            {
                if (AppGlobal.CurrentUser != null)
                {
                    string org = AppGlobal.CurrentUser.OrganizationName;
                    string user = AppGlobal.CurrentUser.Name;
                    string nickname=AppGlobal.CurrentUser.Nickname;

                    //txtUser.Text = string.IsNullOrEmpty(org) ? user : org + "  " + user;
                    txtUser.Text = string.IsNullOrEmpty(nickname) ? user : nickname;
                }
                BrokerBase.Lib.Client.IEdition ed = EditionUtil.GetEditionByEnum((BrokerBase.Lib.Client.EditionType)LicenseInfo.GetInstance().MaxLicenseType);

                tbkVerson.Text = EditionUtil.GetEditionByEnum((BrokerBase.Lib.Client.EditionType)LicenseInfo.GetInstance().MaxLicenseType).Name;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                AppGlobal.Log.Error(e.Message + Environment.NewLine + e.StackTrace);
            }
        }


        private void mi_Click(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            string name = (sender as RadMenuItem).Name;

            if (name == "miSetting")
            {
                ConfigForm frm = new ConfigForm();
                frm.Owner = this;
                frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                frm.ShowDialog();
            }
            else if (name == "miRegInfo")
            {
                UserInfoForm frm = new UserInfoForm();
                frm.Owner = this;
                frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;

                frm.ShowDialog();
            }
            else if (name == "miScoreInfo")
            {
                ScoreForm frm = new ScoreForm();
                frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                frm.Owner = this;
                frm.ShowDialog();
            }
            else if (name == "miUpdate")
            {
                UpdateForm frm = new UpdateForm();

                UpdatePage page = new UpdatePage();
                frm.frmContent.Content = page;
                frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                frm.Owner = this;
                frm.ShowDialog();
            }
            else if (name == "miAbout")
            {
                AboutForm frm = new AboutForm();
                frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                frm.Owner = this;
                frm.ShowDialog();
            }
            else if (name == "miExit")
            {
                if (MessageBox.Show("是否退出美联达保险展业软件", "提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    this.Close();
                }
            }
        }

        private bool Login()
        {
            try
            {
                IList<SysUser> users = DBServiceUtil.GetService<SysUser>(AppGlobal.HbmSessionFactory.GetCurrentSession()).GetListByHQL("from SysUser").ToList<SysUser>();

                if (LicenseInfo.GetInstance().IsEffective)
                {
                    if (users.Count > 0 && AppGlobal.ClientCfg.IsLogin)
                    {
                        LoginForm frm = new LoginForm(users[0].Name);
                        frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                        frm.Owner = this;
                        frm.ResizeMode = ResizeMode.NoResize;
                        frm.ShowDialog();

                        return frm.IsSuccess;
                    }
                    else
                    {
                        return true;
                    }
                }
                else
                {
                    RegPromptForm frm = new RegPromptForm(this);
                    frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                    frm.ResizeMode = ResizeMode.NoResize;
                    frm.ShowDialog();

                    return true;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }

        void DataWCF_ClientRegInfoChanged(BrokerBase.Lib.Client.ClientDataInfo info)
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                if (AppGlobal.ResultClientRegInfo.IsNewData)
                {
                    tbkUpdateInfo.Text = "有新的数据可以下载!";
                }
                else
                {
                    tbkUpdateInfo.Text = null;
                }
            }));
        }

    }
}
